//
//  CorgiHomeTableViewController.swift
//  weibo
//
//  Created by Corgi on 17/3/19.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import YYModel

let cell_ID = "cell_ID"

class CorgiHomeTableViewController: CorgiBaseTableViewController {

    var homeViewModel: CorgiHomeViewModel = CorgiHomeViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavBar()
        
        if !userLogin {
            
            visitorView.updateUI(title: "关注一些人,看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHomeVC: true)
            
            return
        }
        
        setupUI()
        
        loadData()
        
        setupTableView()
    }
    
    private func setupUI() {
    
        navigationController?.view.insertSubview(pullDownTipLabel, belowSubview: navigationController!.navigationBar)
    
    }
    
    //菊花
    fileprivate lazy var footView: UIActivityIndicatorView = {
    
        let view = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    
        view.color = THEMECOLOR
        
        return view
    }()
    
    fileprivate lazy var corgiRefreshControl: CorgiRefreshControl = {
    
        let view = CorgiRefreshControl()
        
        view.addTarget(self, action: #selector(refreshAction), for: .valueChanged)
        
        return view
    }()

    fileprivate lazy var pullDownTipLabel: UILabel = {
    
        let lab = UILabel()
    
        lab.frame = CGRect(x: 0, y: 64-30, width: screenWidth, height: 30)
        
        lab.backgroundColor = THEMECOLOR
       
        lab.textColor = UIColor.white
        
        lab.font = UIFont.systemFont(ofSize: 16)
        
        lab.textAlignment = .center
        
        lab.isHidden = true
        
        return lab
    }()
}

extension CorgiHomeTableViewController {

    @objc fileprivate func refreshAction() {
        
        loadData()
    }
}

///加载数据
extension CorgiHomeTableViewController {

    fileprivate func loadData() {
        
        homeViewModel.getHomeData(isPullUP: footView.isAnimating) { (isSuccess, count) in
            
            
            if !isSuccess {
                
                
                
            } else {
            
                self.tableView.reloadData()
                
                if !self.footView.isAnimating {
                    
                    self.showPullDownTipLabel(count: count)
                }
            }
            
            self.endRefreshing()
        }
    }
    
    fileprivate func showPullDownTipLabel(count: Int) {
        
        if self.pullDownTipLabel.isHidden == false {
            
            return
        }
        
        self.pullDownTipLabel.isHidden = false
        
        var text = ""
        
        if count <= 0 {
            
            text = "已经是最新的微博"
        } else {
            
            text = "您更新了\(count)条微博"
        }
        
        self.pullDownTipLabel.text = text
        
        UIView.animate(withDuration: 1, animations: {
            
            self.pullDownTipLabel.transform = CGAffineTransform(translationX: 0, y: 30)
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, delay: 1, options: [], animations: {
                
                self.pullDownTipLabel.transform = CGAffineTransform.identity
                
            }, completion: { (_) in
                
                self.pullDownTipLabel.isHidden = true
            })
        }
    }
    
    fileprivate func endRefreshing() {
        self.footView.stopAnimating()
        
        self.corgiRefreshControl.endRefreshing()
    }
}

extension CorgiHomeTableViewController {

    fileprivate func setupTableView() {
        
        tableView.register(CorgiHomeCell.self, forCellReuseIdentifier: cell_ID)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 200
        
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = footView
    
        tableView.addSubview(corgiRefreshControl)
    }
}

extension CorgiHomeTableViewController {

    ///设置navBar按钮
    fileprivate func setupNavBar() {
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(pushClick))
        
    }
    
    ///navBar按钮方法
    @objc fileprivate func pushClick() {
        
        let testVC = CorgiTestViewController()
        
        navigationController?.pushViewController(testVC, animated: true)
    }
    
}


// MARK: - Table view data source
extension CorgiHomeTableViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return homeViewModel.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cell_ID, for: indexPath) as! CorgiHomeCell
        
        //cell.backgroundColor = randomColor()
        cell.selectionStyle = .none
        
        cell.statusViewModel = homeViewModel.dataArray[indexPath.row]
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == homeViewModel.dataArray.count - 1 && !footView.isAnimating {
        
            footView.startAnimating()
        
            loadData()
        }
        
    }
}
